<template>
    <view class="register-box">
        <view class="register-back">
            <uni-icons type="arrow-left" size="30" @click="previousStep()"></uni-icons>
        </view>
        <SexAge v-show="state.step == 1" ref="SexAgeRef" @update:sexAgeData="ev => personData.sexAgeData = ev" />
        <nicknameHead v-show="state.step == 2" ref="nicknameHeadRef"
            @update:nicknameHeadDataFn="ev => personData.nicknameHeadData = ev" />
        <PersonalLabel v-show="state.step == 3" ref="PersonalLabelRef"
            @update:updateUserTagFn="ev => personData.checkLabel = ev" />
        <PersonalSignature v-show="state.step == 4" ref="PersonalSignatureRef"
            @update:personalSignatureFn="ev => personData.signature = ev" />
        <view>
            <van-button type="primary" :loading="state.isLoading" @click="nextFn()">下一步</van-button>
        </view>
    </view>
</template>

<script setup>
import { reactive, ref, getCurrentInstance, onMounted } from "vue"
import SexAge from "./components/sexAge/index.vue"
import nicknameHead from "./components/nicknameHead/index.vue"
import PersonalLabel from "./components/personalLabel/index.vue"
import PersonalSignature from "./components/personalSignature/index.vue"
import { getAuthorization, setAuthorization } from "@/utils/auth.js"

import {
    UerAllTags, // 更新tag
} from '@/api/register/index.js'

let { proxy } = getCurrentInstance()


const state = reactive({
    step: 1,
    isLoading: false
})


// import {
//     userOnLine, // 刷新用户登录时间
// } from "@/api/login"
// const getCityFn =async () => {
//     let params = {
//         "ip": "127.0.0.1",
//         "mac": "E:BD:EA:7D:66:7C",
//         "devCode": "c7982d48-49b9-36bf-b049-ee13f1cd5c8f",
//         "imei": "string",
//         "appVersion": "1.0.0",
//         "devModel": "string",
//         "devBrand": "string"
//     }
//     await proxy.$put(userOnLine,params)
// }
onMounted(() => {
    // getCityFn() // 获取所有地址
})

const personData = reactive({
    sexAgeData: {
        Sex: null,
        Birthday: "",
        CityText: ""
    },
    nicknameHeadData: {
        ShowName: "", // 显示名称
        Avatar: "", //头像
        JobID: null, // 职业
        Height: null, // 身高
        Weight: null, // 体重
    },
    checkLabel: {
        "value": [
            { "state": 0, "tagGroupCode": "JYPH", "tags": [] },
            { "state": 0, "tagGroupCode": "ZWMS", "tags": [] },
            { "state": 0, "tagGroupCode": "XQSC", "tags": [] }
        ]
    },
    signature: {
        InviteCode: "",
        Intro: "",
        Wx: "",
        State: "",
    }
})

let SexAgeRef = ref()
let nicknameHeadRef = ref()
let PersonalLabelRef = ref()
let PersonalSignatureRef = ref()
const nextFn = async () => {
    if (state.step == 1) {
        SexAgeRef.value.sexAgeDataFn()
        let { sexAgeData } = personData
        if (sexAgeData.Sex == null || !sexAgeData.Birthday || !sexAgeData.CityText) {
            uni.showToast({
                title: '请填写完基础信息',
                icon: "none"
            })
            return
        }
    }
    if (state.step == 2) {
        nicknameHeadRef.value.nicknameHeadData()
        let { nicknameHeadData } = personData
        if (!nicknameHeadData.ShowName || !nicknameHeadData.Avatar || !nicknameHeadData.JobID || !nicknameHeadData.Height || !nicknameHeadData.Weight) {
            uni.showToast({
                title: '请填写完基础信息',
                icon: "none"
            })
            return
        }
    }
    if (state.step == 3) {
        PersonalLabelRef.value.updateUserTag()
        let { checkLabel } = personData
        let JYPH = checkLabel.value[0].tags.length
        let ZWMS = checkLabel.value[1].tags.length
        let XQSC = checkLabel.value[2].tags.length
        if (JYPH == 0 || ZWMS == 0 || XQSC == 0) {
            uni.showToast({
                title: '请选择标签',
                icon: "none"
            })
            return
        }
        state.isLoading = true
        try {
            let data = await proxy.$put(UerAllTags(), personData.checkLabel)
            state.isLoading = false
        } catch (error) {
            state.isLoading = false

        }
    }
    if (state.step == 4) {
        PersonalSignatureRef.value.updateUserTag()
        let { signature } = personData
        if (!signature.Wx) {
            uni.showToast({
                title: '请填写社交账号',
                icon: "none"
            })
            return
        }
        updateUserInfo()
        // setAuthorization("active", 0)
        // uni.navigateTo({
        //     url: '/pages/home/index'
        // })
    }
    if (state.step <= 3) state.step++
}

import {
    UserInfo
} from "@/api/person/index.js"

const updateUserInfo = async () => {
    let { sexAgeData, nicknameHeadData, signature } = personData
    let params = {
        "Value": []
    }
    const addFn = (persondata) => {
        for (let item in persondata) {
            if (persondata[item]) params.Value.push({ "op": "replace", "path": item, "value": persondata[item] })
        }
    }
    addFn(sexAgeData)
    addFn(nicknameHeadData)
    addFn(signature)
    try {
        state.isLoading = true
        let data = await proxy.$patch(UserInfo + getAuthorization('userId'), params)
        state.isLoading = false
    } catch (error) {
        state.isLoading = false
    }
    setAuthorization("active", 0)
    uni.navigateTo({
        url: '/pages/home/index'
    })
}

const previousStep = () => {
    if (state.step == 1) {
        uni.navigateTo({
            url: '/pages/login/mobileNumberLogin/index'
        })
    }
    if (state.step >= 2) state.step--
}

</script>

<style lang="scss" scoped>
.register-box {
    padding: 30rpx 70rpx;
    
}

.van-button {
    width: 100%;
    margin-top: 60rpx;
    font-size: 36rpx;
    height: 100rpx;
    line-height: 100rpx;
}
</style>
